/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import _entity.CategoryInfo;
import _entity.ProductInfo;
import entity.Categories;
import entity.Products;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author HP
 */
@Stateless
public class CategoriesBean implements CategoriesBeanRemote {

    @PersistenceUnit(unitName = "_asmPU")
    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public List<CategoryInfo> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Categories.class));
        List<Categories> list = getEntityManager().createQuery(cq).getResultList();
        List<CategoryInfo> result = new ArrayList<CategoryInfo>();

        for (Categories c : list) {
            CategoryInfo item = new CategoryInfo();

            List<ProductInfo> pList = new ArrayList<ProductInfo>();

            for (Products p : c.getProductsList()) {
                ProductInfo pItem = new ProductInfo();
                pItem.setDescription(p.getDescription());
                pItem.setId(p.getId());
                pItem.setName(p.getName());
                pItem.setPrice(p.getPrice());
                pItem.setCategory(item);

                pList.add(pItem);
            }

            item.setId(c.getId());
            item.setName(c.getName());
            item.setProductsList(pList);

            result.add(item);
        }

        return result;
    }

    @Override
    public List<CategoryInfo> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Categories.class));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        List<Categories> list = q.getResultList();
        List<CategoryInfo> result = new ArrayList<CategoryInfo>();

        for (Categories c : list) {
            CategoryInfo item = new CategoryInfo();

            List<ProductInfo> pList = new ArrayList<ProductInfo>();
            for (Products p : c.getProductsList()) {
                ProductInfo pItem = new ProductInfo();
                pItem.setDescription(p.getDescription());
                pItem.setId(p.getId());
                pItem.setName(p.getName());
                pItem.setPrice(p.getPrice());
                pItem.setCategory(item);

                pList.add(pItem);
            }
            item.setId(c.getId());
            item.setName(c.getName());
            item.setProductsList(pList);

            result.add(item);
        }

        return result;
    }

    @Override
    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<Categories> rt = cq.from(Categories.class);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public CategoryInfo find(Object id) {
        Categories c = getEntityManager().find(Categories.class, id);
        if (c != null) {
            CategoryInfo result = new CategoryInfo();

            List<ProductInfo> pList = new ArrayList<ProductInfo>();
            for (Products p : c.getProductsList()) {
                ProductInfo pItem = new ProductInfo();
                pItem.setDescription(p.getDescription());
                pItem.setId(p.getId());
                pItem.setName(p.getName());
                pItem.setPrice(p.getPrice());
                pItem.setCategory(result);

                pList.add(pItem);
            }

            result.setId(c.getId());
            result.setName(c.getName());
            result.setProductsList(pList);

            return result;
        }
        return null;
    }

    @Override
    public void edit(CategoryInfo category) {
        Categories c = new Categories();

        List<Products> pList = new ArrayList<Products>();
        for (ProductInfo p : category.getProductsList()) {
            Products pItem = new Products();
            pItem.setDescription(p.getDescription());
            pItem.setId(p.getId());
            pItem.setName(p.getName());
            pItem.setPrice(p.getPrice());
            pItem.setCategories(c);

            pList.add(pItem);
        }

        c.setId(category.getId());
        c.setName(category.getName());
        c.setProductsList(pList);

        getEntityManager().merge(c);
    }

    @Override
    public void create(CategoryInfo category) {
        Categories c = new Categories();

        List<Products> pList = new ArrayList<Products>();
        for (ProductInfo p : category.getProductsList()) {
            Products pItem = new Products();
            pItem.setDescription(p.getDescription());
            pItem.setId(p.getId());
            pItem.setName(p.getName());
            pItem.setPrice(p.getPrice());
            pItem.setCategories(c);

            pList.add(pItem);
        }

        c.setId(category.getId());
        c.setName(category.getName());
        c.setProductsList(pList);

        getEntityManager().persist(c);
    }

    @Override
    public void remove(CategoryInfo category) {
        Categories c = new Categories();

        List<Products> pList = new ArrayList<Products>();
        for (ProductInfo p : category.getProductsList()) {
            Products pItem = new Products();
            pItem.setDescription(p.getDescription());
            pItem.setId(p.getId());
            pItem.setName(p.getName());
            pItem.setPrice(p.getPrice());
            pItem.setCategories(c);

            pList.add(pItem);
        }

        c.setId(category.getId());
        c.setName(category.getName());
        c.setProductsList(pList);
        getEntityManager().remove(getEntityManager().merge(c));
    }

    @Override
    public Categories find_2(Object id) {
        Categories c = getEntityManager().find(Categories.class, id);
        if (c != null) {
            return c;
        }
        return null;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
